package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysUserCv;
import cn.wgx.modules.sys.entity.SysUserCvExample;
import cn.wgx.modules.sys.entity.mapper.SysUserCvMapper;
import cn.wgx.modules.sys.util.UserUtil;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysUserCvService extends BaseService<SysUserCv> {



    @Resource
    SysUserCvMapper mapper;

    @Transactional(readOnly = true)
    public List<SysUserCv> getList(SysUserCv record, Boolean desc) {
		SysUserCvExample example = new SysUserCvExample();
		if(record != null){
            SysUserCvExample.Criteria criteria = example.createCriteria();
            if(Validation.isNotNullOrZero(record.getUser_id())){
                criteria.andUser_idEqualTo(record.getUser_id());
            }
        }
		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysUserCv> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysUserCvExample example = new SysUserCvExample();
            SysUserCvExample.Criteria criteria = example.createCriteria();
            criteria.andCv_idIn(uniList);
            List<SysUserCv> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }

    @Transactional(readOnly = true)
    public SysUserCv getOne(Integer id) {
        SysUserCv record = new SysUserCv();
        record.setUser_id(id);
        List<SysUserCv> list = getList(record, true);
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Transactional
    public SysUserCv insertOne( SysUserCv record) {
        filter(record, true);
        mapper.insertSelective(record);
        return record;
    }


    @Transactional
    public SysUserCv updateOne(SysUserCv record) {
        filter(record, false);
        SysUserCvExample example = new SysUserCvExample();
        example.createCriteria().andUser_idEqualTo(UserUtil.getUser().getUser_id());
        mapper.updateByExampleSelective(record, example);
        return record;
    }


    @Transactional
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

	public void filter(SysUserCv record,  boolean insert){
        if(insert){

        }else{
            record.setUser_id(null);
        }
        super.filter(record, insert);
    }

}